package com.sphd.beautysalon.activitys.marking;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.sphd.beautysalon.info.LoginInfo;
import com.sphd.beautysalon.R;
import com.sphd.beautysalon.base.BaseActivity;
import com.sphd.beautysalon.base.RestaurantUrl;
import com.sphd.beautysalon.info.CardInfo;
import com.sphd.beautysalon.info.RestaurantInfo;
import com.sphd.beautysalon.presenter.EticketPresenterImpl;
import com.sphd.beautysalon.utils.Constant;
import com.sphd.beautysalon.view.EticketView;

import org.litepal.crud.DataSupport;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 电子优惠券设置界面
 */
public class EticketActivity extends BaseActivity implements EticketView {

    @BindView(R.id.action_addeticket)
    ImageView actionAddeticket;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.etickets_switch)
    Switch eticketsSwitch;
    @BindView(R.id.give_etickets_switch)
    Switch giveEticketsSwitch;
    @BindView(R.id.get_eticket)
    TextView getEticket;
    @BindView(R.id.v_line_img)
    ImageView vLineImg;
    @BindView(R.id.eticket_money)
    TextView eticketMoney;
    @BindView(R.id.eticket_condition)
    TextView eticketCondition;
    @BindView(R.id.eticket_date)
    TextView eticketDate;
    @BindView(R.id.content_eticket)
    LinearLayout contentEticket;
    RestaurantInfo restaurantInfo;
    LoginInfo loginInfo;
    @BindView(R.id.quan_layout)
    LinearLayout quanLayout;
    private Intent intent;
    private Bundle bundle;
    EticketPresenterImpl eticketPresenter;
    CardInfo cardInfo;
    boolean isDel=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_eticket);
        ButterKnife.bind(this);
        initView();
        initEnent();
    }

    private void initEnent() {
        toolbar.setNavigationOnClickListener(view -> finish());
        eticketsSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!buttonView.isPressed()) {
                    return;
                }
                if (isChecked) {
                    toast("点击右上角新增优惠券进行设置");
                } else {
                    isDel=true;
                    eticketPresenter.doResUel(RestaurantUrl.Httptitle + RestaurantUrl.SwitchEtickets + "isyh=1&cgid=" + restaurantInfo.getId());
                }
            }
        });
        giveEticketsSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //当按钮被按下时会触发此listener
                if (!buttonView.isPressed()) {
                    return;
                }
                if (isChecked) {
                    toggleButtonEtickets(1);
                } else {
                    toggleButtonEtickets(0);
                }


            }
        });
    }

    private void toggleButtonEtickets(int i) {
        eticketPresenter.doResUel(RestaurantUrl.Httptitle +
                RestaurantUrl.GiveEtickets + "cgid=" + restaurantInfo.getId() + "&isgive=" + i);

    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        setTitleBar(toolbar, "电子优惠券");
        loginInfo = DataSupport.findLast(LoginInfo.class);
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        eticketPresenter = new EticketPresenterImpl(this);
        getTickMsg();
    }

    private void getTickMsg() {
        eticketPresenter.getResInfo(RestaurantUrl.Httptitle + RestaurantUrl.RESTAURANT + "cgid=" + restaurantInfo.getId());
    }

    @Override
    public void recMsg(RestaurantInfo restaurantInfo) {
        if (restaurantInfo != null) {
            this.restaurantInfo = restaurantInfo;
            initSwitch();
            addData();
        } else {
            toast("数据错误，请稍后重试");
        }
    }

    private void initSwitch() {
        if (isCg_has_storemanager(restaurantInfo) && isSuperManager(loginInfo)) {
            giveEticketsSwitch.setEnabled(false);
            eticketsSwitch.setEnabled(false);

        } else {
            giveEticketsSwitch.setEnabled(true);
            eticketsSwitch.setEnabled(true);
        }

        eticketsSwitch.setChecked(restaurantInfo.isCg_isyh());
        giveEticketsSwitch.setChecked(restaurantInfo.isCg_isgive());


    }

    private void addData() {
        eticketPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.ShowEtickets + "cgid=" + restaurantInfo.getId());
    }

    @OnClick(R.id.action_addeticket)
    public void onClick() {

        bundle.putSerializable(Constant.CARDINFO,cardInfo);
        bundle.putSerializable(Constant.RESTAURANTSINFO,restaurantInfo);
        intent.putExtras(bundle);
        intent.setClass(context,AddEticketActivity.class);
        startActivityForResult(intent,1);

    }

    @Override
    public void recGoupon(CardInfo cardInfo) {
        this.cardInfo=cardInfo;
        if (cardInfo == null) {
            quanLayout.setVisibility(View.GONE);
            toast("暂无优惠券");
        } else {
            quanLayout.setVisibility(View.VISIBLE);
            eticketDate.setText("剩余有效天数:" + cardInfo.getCard_limitdays());
            eticketCondition.setText("满" + cardInfo.getCard_upperlimit() + "元使用");
            eticketMoney.setText("￥" + cardInfo.getCard_value() + "元");
            getEticket.setText("获取条件:消费" + cardInfo.getCard_givelimit() + "元");
        }
    }

    @Override
    public void recOk() {
        toast("操作成功");
        if (isDel){
            //添加完成后开启优惠券按钮
            // TODO: 2016/12/16  
            cardInfo=null;
            quanLayout.setVisibility(View.GONE);
            isDel=false;
        }
    }

    @Override
    public void recFaile() {
        toast("操作失败，请重试");
        isDel=false;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==1){
            getTickMsg();
        }
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
    }
}
